package cn.dnui_001zjs.controller;

import java.io.File;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.dnui_001zjs.po.Category;
import cn.dnui_001zjs.po.Trip;
import cn.dnui_001zjs.po.TripComment;
import cn.dnui_001zjs.service.CategoryService;
import cn.dnui_001zjs.service.TripService;

@Controller
public class TripController {
	@Autowired
	private TripService tripService;
	
	@Autowired
	private CategoryService categoryService;
	
	@RequestMapping("/admin/listtrip")
	public String listtrip(Model model){
		List<Trip> triplist = tripService.getTripList();
		model.addAttribute("triplist",triplist);
		List<Category> catlist = categoryService.getCatList();
		model.addAttribute("catlist",catlist);
		return "admin/triplist";
		
	}
	@RequestMapping("/admin/listtrip1")
	public String listtrip1(Model model){
		List<Trip> triplist = tripService.getTripList();
		model.addAttribute("triplist",triplist);
		return "admin/triplist1";
		
	}
	@RequestMapping("/admin/tripadd")
	public String addtrip(Model model){
		List<Category> catlist = categoryService.getCatList();
		model.addAttribute("catlist",catlist);
		
		return "admin/tripadd";
	}
	
	
	
	@RequestMapping("/admin/deltrip")
	public String delTrip(Integer tripid){
		int num;
		num = tripService.delTripByID(tripid);
		if(num<=0)
			return "forward:listtrip.action";
		else
			return "redirect:listtrip.action";
		
	}
	@RequestMapping("/admin/deltripmodal")
	@ResponseBody
	public String delTripModal(Integer tripid){
		int num;
		num = tripService.delTripByID(tripid);
		if(num<=0)
			return "FAIL";
		else
			return "OK";
		
	}
	
	
	@RequestMapping("/admin/tripedit")
	public String tripedit(Integer tripid,Model model){
		Trip trip=new Trip();
		trip = tripService.getTripById(tripid);
		model.addAttribute("trip", trip);
		List<Category> catlist = categoryService.getCatList();
		model.addAttribute("catlist", catlist);
		return "admin/tripedit";
	}
	
	@RequestMapping("/admin/tripeditmodal")
	@ResponseBody
	public Trip tripeditmodal(Integer tripid){
		Trip trip=new Trip();
		trip = tripService.getTripById(tripid);
		
		return trip;
	}
	
	@RequestMapping(value="/admin/updatetrip",method=RequestMethod.POST)
	public String updateTrip(Trip trip){
		int num;
		num = tripService.updateTrip(trip);
		return "redirect:/admin/listtrip.action";
		
	}
	
	@RequestMapping(value="/admin/updatetripmodal",method=RequestMethod.POST)
	@ResponseBody
	public String updateTripModal(Trip trip){
		int num;
		num = tripService.updateTrip(trip);
		if(num<=0) return "FAIL";
		else return "OK";
		
	}
	
	@RequestMapping(value="/admin/addtrip",method=RequestMethod.POST)
	public String addTrip(@RequestParam("trippic") List<MultipartFile> trippic,
			HttpServletRequest request){
		// 判断所上传文件是否存在
	if (!trippic.isEmpty() && trippic.size() > 0) {
		//循环输出上传的文件
		for (MultipartFile file : trippic) {
			// 获取上传文件的原始名称
			String originalFilename = file.getOriginalFilename();
			// 设置上传文件的保存地址目录
			String dirPath = 
                   request.getServletContext().getRealPath("/uploads/imgs/");
			File filePath = new File(dirPath);
			System.out.println("=========dirPath="+dirPath);
			// 如果保存文件的地址不存在，就先创建目录
			if (!filePath.exists()) {
				System.out.println("==========");
				filePath.mkdirs();
			}
			// 使用UUID（通用唯一标识）重新命名上传的文件名称(上传人_uuid_原始文件名称)
			String newFilename = UUID.randomUUID()+originalFilename;
			try {
				// 使用MultipartFile接口的方法完成文件上传到指定位置
				file.transferTo(new File(dirPath + newFilename));
				System.out.println(dirPath + newFilename);
				//
				Trip trip = new Trip();
				trip.setCid(Integer.parseInt(request.getParameter("cid")));
				trip.setTripname(request.getParameter("tripname"));
				trip.setTripdesc(request.getParameter("tripdesc"));
				trip.setTrippic("uploads/imgs/"+newFilename);
				int num = 0;
				num =tripService.addTrip(trip);
				if(num<=0)
					return "redirect:tripadd.action";
				else
					return "redirect:listtrip.action";
			} catch (Exception e) {
				e.printStackTrace();
                   return"error";
			}
		}				
	}
	else{
		System.out.println("nothing");
		return "redirect:tripadd.action";
	}
	return "forward:index.action";
	}
	
	
	@RequestMapping(value="/admin/addtripmodal",method=RequestMethod.POST)
	@ResponseBody
	public String addTripModel(@RequestParam("trippic") List<MultipartFile> trippic,
			HttpServletRequest request){
		// 判断所上传文件是否存在
	if (!trippic.isEmpty() && trippic.size() > 0) {
		//循环输出上传的文件
		for (MultipartFile file : trippic) {
			// 获取上传文件的原始名称
			String originalFilename = file.getOriginalFilename();
			// 设置上传文件的保存地址目录
			String dirPath = 
                   request.getServletContext().getRealPath("/uploads/imgs/");
			File filePath = new File(dirPath);
			System.out.println("=========dirPath="+dirPath);
			// 如果保存文件的地址不存在，就先创建目录
			if (!filePath.exists()) {
				System.out.println("==========");
				filePath.mkdirs();
			}
			// 使用UUID（通用唯一标识）重新命名上传的文件名称(上传人_uuid_原始文件名称)
			String newFilename = UUID.randomUUID()+originalFilename;
			try {
				// 使用MultipartFile接口的方法完成文件上传到指定位置
				file.transferTo(new File(dirPath + newFilename));
				System.out.println(dirPath + newFilename);
				//
				Trip trip = new Trip();
				trip.setCid(Integer.parseInt(request.getParameter("cid")));
				trip.setTripname(request.getParameter("tripname"));
				trip.setTripdesc(request.getParameter("tripdesc"));
				trip.setTrippic("uploads/imgs/"+newFilename);
				int num = 0;
				num =tripService.addTrip(trip);
				if(num<=0)
					return "FAIL";
				else
					return "OK";
			} catch (Exception e) {
				e.printStackTrace();
                   return"error";
			}
		}				
	}
	else{
		//System.out.println("nothing");
		return "FAIL";
	}
	return "FAIL";
	}
	
	@RequestMapping("/admin/getComment")
	@ResponseBody
	public List<TripComment> getComment(){
		List<TripComment> list = tripService.getComment();
		
		return list;
	}
	
}
